package 消息队列.rabbitmq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 订单的消费者
 */
@Component
public class BillConsumer {
    //concurrency = "1" 方法运行在单线程中
    @RabbitListener(queues = DelayExchangeConfig.QUEUE_NAME,concurrency = "1")
    public void handleBill(ChargingBill chargingBill, Channel channel , Message message) throws  Throwable{
        System.out.println("线程号="+Thread.currentThread().getId());
        System.out.println(chargingBill.toString());
        try{
            //更新数据库
            //更新成功，发确认信息，消息队列收到确认信息会删除数据
            long tag = message.getMessageProperties().getDeliveryTag();
            //tag的值是5，消息队列服务器认为5前面的1,2,3,4，消费者也收到了，
            channel.basicAck(tag,true);
        }catch (Exception e){
            //不发确认信息，消息队列没有收到确认信息，信息还在队列中
        }

    }
}
